package com.atgm.mall.gmmailorder.interceptor;

import com.atgm.common.constant.AuthConstant;
import com.atgm.common.exception.BizCodeEnume;
import com.atgm.common.exception.CartException;
import com.atgm.common.utils.GmMallUtils;
import com.atgm.common.utils.JwtUtils;
import com.atgm.common.vo.MemberResponseVo;
import io.jsonwebtoken.Jwt;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Component
public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberResponseVo> loginUser = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取登录用户的token
        String token = (String) request.getHeader(AuthConstant.TOKEN);
        if (GmMallUtils.isNotEmpty(token)) {
            Long memberId = JwtUtils.getMemberId(token);
            MemberResponseVo memberResponseVo = new MemberResponseVo();
            memberResponseVo.setId(memberId);
            memberResponseVo.setUsername(JwtUtils.getUsername(token));
            loginUser.set(memberResponseVo);
        }
        return true;
    }


}
